package sk.naive.talker.command;

import sk.naive.talker.*;
import sk.naive.talker.message.*;
import sk.naive.talker.util.Utils;

import java.rmi.RemoteException;
import java.util.Collection;
import java.util.HashSet;

/**
 * Kill kicks bad asses out of the talker.
 *
 * <a href="mailto:maslenka@naive.deepblue.sk">Zuzana "Maslenka" Maslenova</a>
 * @version $Revision: 1.5 $ $Date: 2005/01/25 21:57:04 $
 */
public class Kill extends AbstractCommand {
	public void exec() throws CommandException, RemoteException {
		Utils.checkPermission(user, Consts.PERM_SUPERUSER);
		String[] sa = Utils.splitWords(params, 2);
		if (sa.length < 2) {
			sendMisusageWarning();
			return;
		}
		User victim = findTarget(sa[0]);
		String reason = sa[1];
		if (victim == null) {
			return;
		}

		ctxSet(DefaultMessageFactory.CTXKEY_VAL, reason);
		ctxSet(DefaultMessageFactory.CTXKEY_VAL + "Vctm", victim.getProperties());

		Collection excluded = new HashSet();
		excluded.add(user);
		excluded.add(victim);
		sendMassMessage(getMessage("kill.audience", user.getProperties()), getPossibleTargetUsers(user), excluded, Category.MAGIC);

		victim.send(getString("kill.victimBefore", user.getProperties(), victim.getLang()));
		logger().info(user.getLogin() + " killed " + victim.getLogin() + ". Reason: " + reason);

		if (commandDispatcher.getUserFinder().onlineUsers().contains(user)) {
			user.send(getString("kill.ok", user.getProperties()));
		}
		commandDispatcher.process("quit " + getString("kill.quitMessage", user.getProperties(), victim.getLang()), victim);
	}
}